/**
 * Disable scrolling on iOS
 */
$(document).ready(function() {
	$(document).bind("touchmove", function(event) {
		event.preventDefault();
	});
});
//document.body.addEventListener('touchmove', function(event) {
//	event.preventDefault();
//}, false); 

$(document).ready(function() {
	function Touch(id, target, x, y) {
		this.clientX = x;
		this.clientY = y;
		this.identifier = id;
		this.pageX = x;
		this.pageY = y;
		this.screenX = x;
		this.screenY = y;
		this.target = target;
		this.toString = function() {
			return "{id:"+ this.identification +", t:"+ this.target +", x:"+ this.pageX +", y:"+ this.pageY +"}";
		}
	}

	var touches        = [];
	var targetTouches  = [];
	var changedTouches = [];

	var isTouching = false;
	var source = null;
	var touchStart = null;

	function triggerTouchEvent(type, target) {
//		console.log("touch "+ type +" "+ target.id);
		var event = $.Event(type);
		event.target = target;
		event.touches        = touches;
		event.targetTouches  = targetTouches;
		event.changedTouches = changedTouches;
		$(target).trigger(event);
	}
	function triggerMouseEvent(type, target) {
//		console.log("mouse "+ type +" "+ target.id);
		var event = $.Event(type);
		event.target = target;
		event.pageX = 0;
		event.pageY = 0;
		$(target).trigger(event);
	}

	$(document).on("mousedown", function(event) {
		isTouching = true;
		touches[0] = new Touch(1, event.target, event.pageX, event.pageY);
		source = event.target;

		touchStart = { x: event.pageX, y: event.pageY };

		triggerTouchEvent("xtouchstart", source);
		triggerMouseEvent("xmousedown", source)
	});
	$(document).on("touchstart", function(event) {
		touches        = event.originalEvent.touches;
		targetTouches  = event.originalEvent.targetTouches;
		changedTouches = event.originalEvent.changedTouches;
		source = touches[0].target;

		touchStart = { x: touches[0].pageX, y: touches[0].pageY };

		triggerTouchEvent("xtouchstart", source);
		triggerMouseEvent("xmousedown", source);
	});

	$(document).on("mousemove", function(event) {
		if (!isTouching) return;

		touches[0] = new Touch(1, event.target, event.pageX, event.pageY);

		triggerTouchEvent("xtouchmove", source);
		triggerMouseEvent("xmousemove", source);
	});
	$(document).on("touchmove", function(event) {
		touches        = event.originalEvent.touches;
		targetTouches  = event.originalEvent.targetTouches;
		changedTouches = event.originalEvent.changedTouches;

		triggerTouchEvent("xtouchmove", source);
		triggerMouseEvent("xmousemove", source);
	});

	$(document).on("mouseup", function(event) {
		isTouching = false;

		changedTouches[0] = new Touch(1, event.target, event.pageX, event.pageY);

		triggerTouchEvent("xtouchend", source);
		triggerMouseEvent("xmouseup", source)

		var dx = event.pageX - touchStart.x;
		var dy = event.pageY - touchStart.y;
//		if (source == changedTouches[0].target) {
		if (Math.abs(dx) <= 4 && Math.abs(dy) <= 4) {
			triggerMouseEvent("xclick", event.target);
		}

		source = null;
	});
	$(document).on("click", function(event) {
//		triggerMouseEvent("xclick", event.target);
	});
	$(document).on("touchend", function(event) {
		touches        = event.originalEvent.touches;
		targetTouches  = event.originalEvent.targetTouches;
		changedTouches = event.originalEvent.changedTouches;

		triggerTouchEvent("xtouchend", source);
		triggerMouseEvent("xmouseup", source);

		var dx = changedTouches[0].pageX - touchStart.x;
		var dy = changedTouches[0].pageY - touchStart.y;
//		if (source == changedTouches[0].target) {
		if (Math.abs(dx) <= 4 && Math.abs(dy) <= 4) {
			triggerMouseEvent("xclick", source);
		}

		source = null;
	});
	$(document).on("touchcancel", function(event) {
		touches        = event.originalEvent.touches;
		targetTouches  = event.originalEvent.targetTouches;
		changedTouches = event.originalEvent.changedTouches;

		triggerTouchEvent("xtouchcancel", source);
		triggerMouseEvent("xmousecancel", source);

		source = null;
	});
});
/*
touchstart
touchmove
touchend
touchcancel

event.touches[]
event.targetTouches[]
event.changedTouches[]

gesturestart
gesturechange
gestureend


mousemove
mouseover
mouseleave
mousedown
mouseup
click
*/

/**
 * Touch Events
 */
/*
$(document).ready(function() {
	$(document).bind("xtouchstart", function(event) {
	});
	$(document).bind("xtouchmove", function(event) {
	});
	$(document).bind("xtouchend", function(event) {
	});
});
*/
